import {
  Entity,
  Column,
  ManyToOne,
  JoinColumn,
  OneToMany,
  BeforeInsert,
  BeforeUpdate,
} from 'typeorm';
import { Group } from 'src/modules/group/entities/group.entity';
import { Playground } from 'src/modules/playground/entities/playground.entity';
import { GameInfoV2 } from 'src/modules/game/entities/game.entity.v2';
import { Event } from 'src/modules/event/entities/event.entity';
import {
  EventTypeEnum,
  MatchPlayerStatusEnum,
  MatchStatusEnum,
} from 'src/common/constant/enums';
import { Category } from 'src/modules/category/entities/category.entity';
import { Stage } from 'src/modules/stage/entities/stage.entity';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';
import { EventToPlayer } from 'src/modules/event-to-player/entities/event-to-player.entity';
import { GameRule } from 'src/modules/game-rule/entities/game-rule.entity';
import { RallyRule } from 'src/modules/rally-rule/entities/rally-rule.entity';
import { BaseEntity } from 'src/common/entity/BaseEntity';

@Entity('match_info')
export class MatchInfoV2 extends BaseEntity {
  @Column()
  groupLoopIndex: number; // 淘汰赛轮次，1表示第一轮，2表示第二轮，以此类推

  @Column()
  type: EventTypeEnum;

  @Column()
  isTeamMatch: boolean;

  @Column({ name: 'homePlayer' })
  homePlayerId: number; // 主场选手编码

  @Column({ name: 'awayPlayer' })
  awayPlayerId: number; // 客场选手编码

  @Column({ name: 'winner' })
  winnerId: number; // 胜利者编码

  @ManyToOne(() => EventToPlayer, {
    cascade: true,
  })
  @JoinColumn({ name: 'homePlayer' })
  homePlayer: EventToPlayer;

  @ManyToOne(() => EventToPlayer, {
    cascade: true,
  })
  @JoinColumn({ name: 'awayPlayer' })
  awayPlayer: EventToPlayer;

  @ManyToOne(() => EventToPlayer)
  @JoinColumn({ name: 'winner' })
  winner: EventToPlayer;

  @Column()
  homePlayerRally: number; // 主场选手得分

  @Column()
  awayPlayerRally: number; // 客场选手得分

  @Column()
  homePlayerStatus: MatchPlayerStatusEnum;

  @Column()
  awayPlayerStatus: MatchPlayerStatusEnum;

  @Column({ default: MatchStatusEnum.Created })
  status: MatchStatusEnum;

  @Column()
  rangeMin: number;

  @Column()
  rangeMax: number;

  @ManyToOne(() => GameRule)
  @JoinColumn({ name: 'fk_gamerule_id' })
  gameRule: GameRule;

  @ManyToOne(() => RallyRule)
  @JoinColumn({ name: 'fk_rallyrule_id' })
  rallyRule: RallyRule;

  @Column({ name: 'fk_gamerule_id' })
  gameRuleId: number;

  @Column({ name: 'fk_rallyrule_id' })
  rallyRuleId: number;

  @Column()
  estimatedStart: Date;

  @Column()
  estimatedEnd: Date;

  @Column()
  actualStart: Date;

  @Column()
  actualEnd: Date;

  @Column()
  groupMatchIndex: number; // 比赛在小组中的索引

  @Column()
  nextWinnerMatchIndex: number; // 淘汰赛中的胜者晋级的比赛索引

  @Column()
  nextLoserMatchIndex: number;

  @Column()
  subMatchOrder: number;

  @Column({ nullable: true, name: 'fk_groupId' }) // 比赛所在的分组
  groupId: number;

  @Column({ name: 'fk_subMatchId' })
  subMatchId: number; // 说是sub 实际是parent...

  @ManyToOne(() => Playground)
  @JoinColumn({ name: 'fk_playgroundId' })
  playground: Playground;

  @Column({ name: 'fk_playgroundId' })
  playgroundId: number;

  @ManyToOne(() => Group, (matchGroup) => matchGroup.matchInfos)
  @JoinColumn({ name: 'fk_groupId' })
  group: Group;

  //subMatchInfos
  @ManyToOne(() => MatchInfoV2, (matchInfo) => matchInfo.subMatchInfos)
  @JoinColumn({ name: 'fk_subMatchId' })
  parentMatchInfo: MatchInfoV2;

  @OneToMany(() => MatchInfoV2, (matchInfo) => matchInfo.parentMatchInfo, {
    cascade: true,
  })
  subMatchInfos: MatchInfoV2[];

  @OneToMany(() => GameInfoV2, (gameInfo) => gameInfo.matchInfo, {
    cascade: true,
  })
  games: GameInfoV2[];

  @BeforeInsert()
  BeforeInsert() {
    this.createTime = new Date();
    this.updateTime = this.createTime;
  }

  @BeforeUpdate()
  BeforeUpdate() {
    this.updateTime = new Date();
  }

  @Column({ name: 'fk_tournamentId' })
  tournamentId: number;

  @ManyToOne(() => Tournament)
  @JoinColumn({ name: 'fk_tournamentId' })
  tournament: Tournament;

  @Column({ name: 'fk_categoryId' })
  categoryId: number;

  @ManyToOne(() => Category)
  @JoinColumn({ name: 'fk_categoryId' })
  category: Category;

  @Column({ name: 'fk_stageId' })
  stageId: number;

  @ManyToOne(() => Stage)
  @JoinColumn({ name: 'fk_stageId' })
  stage: Stage;

  @Column({ name: 'fk_eventId' })
  eventId: number;

  @ManyToOne(() => Event)
  @JoinColumn({ name: 'fk_eventId' })
  event: Event;

  // @Column({ name: 'fk_eventplayerId' }) // 貌似没用
  // eventPlayerId: number;

  // @ManyToOne(() => EventToPlayer) // 貌似没用
  // @JoinColumn({ name: 'fk_eventplayerId' })
  // eventPlayer: EventToPlayer;

  @Column()
  mustFinishMaxCountGame: boolean;

  @Column()
  mustFinishMaxCountMatch: boolean;

  hasByePlayer: boolean;

  @Column({ name: 'copyFromId', select: false })
  copyFromId?: number; // 复制自原记录ID
}
